<template>
  <el-form label-width="120px" :model="formData" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option v-for="item in hireType" :key="item.id" :value="item.id" :label="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        style="width:50%"
        placeholder="请选择部门"
        @focus="focusForm"
      />
      <div v-if="showTree">
        <el-tree :data="treeData" :props="{label:'name'}" @node-click="hNode" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="doAdd">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import formatHire from '@/constant/employees'
import { validMobile } from '@/utils/validate'
import { getDepartments } from '@/api/departments'
import { arr2tree } from '@/utils/index'
import { sendAdd } from '@/api/employees'
const hireType = formatHire.hireType
export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validMobile(value)) {
        callback(new Error('请输入正确手机号'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 2, max: 5, message: '姓名长度不符', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 3, max: 10, message: '工号长度不符', trigger: 'blur' }
        ],
        mobile: [{ required: true, trigger: 'blur', validator: validateUsername }]
      },
      hireType: hireType,
      treeData: [],
      showTree: false
    }
  },
  methods: {
    async focusForm() {
      await this.loadDepartments()
      this.showTree = true
    },
    async loadDepartments() {
      const res = await getDepartments()
      res.data.depts.shift()
      // const res1 = arr2tree(this.list, '')
      // console.log(res1)
      this.treeData = arr2tree(res.data.depts, '')
    },
    hNode(data, node) {
      if (data.children.length) {
        return
      } else {
        this.formData.departmentName = data.name
      }
      this.showTree = false
    },
    async doAdd() {
      await sendAdd(this.formData)
      this.$emit('success')
    }
  }
}
</script>

<style>

</style>
